package com.company;

import java.util.*;

/**
 * Created by yijy on 2017/5/2.
 * 用HashMap对苹果重量排序
 */
public class Solution {

    public static void main(String[] args) {
        Map map = new HashMap<String,Integer>();
        map.put("苹果a",1);
        map.put("苹果c",6);
        map.put("苹果b",3);
        map.put("苹果g",1);
        map.put("苹果d",8);
        map.put("苹果e",10);
        map.put("苹果f",3);

        List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
        //根据key排序
        Collections.sort(list, new Comparator<Map.Entry>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2) {
                return o1.getKey().toString().compareTo(o2.getKey().toString());
            }
        });
        System.out.println("************根据key排序结果************");
        for(Map.Entry entry:list){
            System.out.print(entry.getKey()+"\t");
        }
        System.out.println();
        //根据value排序
        Collections.sort(list, new Comparator<Map.Entry<String,Integer>>() {
            @Override
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getValue()-o2.getValue();
            }
        });
        System.out.println("************根据value排序结果************");
        for(Map.Entry entry:list){
            System.out.print(entry.getValue()+"\t");
        }

    }
}
